package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;

import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class LoginByPCViewModel extends BaseViewModel {
    public String content;
    public LoginByPCViewModel(@NonNull Application application) {
        super(application);
    }

    //登录的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(() -> {
        toScanLogin(content);
    });
    //取消点击事件
    public BindingCommand cancelOnClickCommand = new BindingCommand(() -> {
        finish();
    });

    public void toScanLogin(String content) {
        showProgress();
        TelegramManager.getTelegramManager().confirmQrCodeAuthentication(getApplication(), content, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if(obj.ok){
                    showErrorTip("扫码登录成功");
                }else{
                    showErrorTip(obj.errString);
                }
                finish();

            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip("无效二维码");
                finish();
            }
        });
    }
}
